﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QAction>
#include <QLabel>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //将菜单栏加到QMainWindow主窗体中
    this->setMenuBar(ui->menuBar);

    /* 创建多个菜单 */
    QMenu *FileMenu = new QMenu(tr("文件"),this);
    QMenu *EditMenu = new QMenu(tr("编辑"),this);

    /* 将菜单添加到菜单栏中 */
    ui->menuBar->addMenu(FileMenu);
    ui->menuBar->addMenu(EditMenu);

    /* 创建多个菜单项 */
    QAction *newAction = new QAction(QIcon(":/mainwindow/icon/新建.png"),tr("新建 ..."),this);
    QAction *openAction = new QAction(QIcon(":/mainwindow/icon/打开.png"),tr("打开 ..."),this);
    QAction *saveAction = new QAction(QIcon(":/mainwindow/icon/保存.png"),tr("保存"),this);

    QAction *copyAction = new QAction(QIcon(":/mainwindow/icon/复制.png"),tr("复制"),this);
    QAction *pasteAction = new QAction(QIcon(":/mainwindow/icon/粘贴.png"),tr("粘贴"),this);
    QAction *cutAction = new QAction(QIcon(":/mainwindow/icon/剪切.png"),tr("剪切"),this);

    /* 将菜单项添加到菜单中 */
    FileMenu->addAction(newAction);
    FileMenu->addAction(openAction);
    FileMenu->addAction(saveAction);

    EditMenu->addAction(copyAction);
    EditMenu->addAction(pasteAction);
    EditMenu->addAction(cutAction);

    /* 将菜单添加到工具栏中 */
    ui->toolBar->addAction(newAction);
    ui->toolBar->addAction(openAction);
    ui->toolBar->addAction(saveAction);

    //为菜单项添加快捷方式
    newAction->setShortcut(QKeySequence("Ctrl+N"));
    openAction->setShortcut(QKeySequence("Ctrl+O"));
    saveAction->setShortcut(QKeySequence("Ctrl+S"));
    copyAction->setShortcut(QKeySequence("Ctrl+C"));
    pasteAction->setShortcut(QKeySequence("Ctrl+V"));
    cutAction->setShortcut(QKeySequence("Ctrl+X"));

    //状态栏
    //this->setStatusBar(ui->statusBar);

    LeftLabel = new QLabel("左侧提示信息",this);
    RightLabel = new QLabel("右侧提示信息",this);

    ui->statusBar->addWidget(LeftLabel);    /* 状态栏左侧添加控件 */
    ui->statusBar->addPermanentWidget(RightLabel);   /* 状态栏右侧添加控件 */

    /* 将文本编辑框作为QMainWindow的中心部件 */
    setCentralWidget(ui->textEdit);


    //菜单信号与槽的连接
    connect(openAction, &QAction::triggered, this, &MainWindow::openFile);
    connect(ui->textEdit, &QTextEdit::cursorPositionChanged, this, &MainWindow::setStatus);


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::openFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("打开文件"),
                                                    "./test_source",
                                                    tr("文本文件 (*.txt)"));

    /* 构建文件类对象 */
    QFile file(fileName);

    /* 以只读模式打开文本文件 */
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    QTextStream in(&file);                  /* 将文件转换为文本流 */
    ui->textEdit->setText(in.readAll());        /* 文本流中读取所有数据写入到QtextEdit控件中 */
}

void MainWindow::saveFile()
{

}

void MainWindow::setStatus()
{
    QTextCursor textCursor = ui->textEdit->textCursor();
    QString str = QString::number(textCursor.blockNumber() + 1) + ":" + QString::number(textCursor.columnNumber());
    RightLabel->setText(str);
//    LeftLabel->setNum(ui->textEdit->textCursor().columnNumber());
//    RightLabel->setNum(ui->textEdit->textCursor().blockNumber());

}
